//
//  DataBaseManager.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/25.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "DataBaseManager.h"
#import <FMDB.h>


@interface DataBaseManager ()
//数据库对象（作用类似：sqlite中dbPoint）
@property (nonatomic,retain)FMDatabase *fmdb;
@end


@implementation DataBaseManager
#pragma mark - ------------ 内存管理 ---------------

-(void)dealloc{
    
    [_fmdb release];
    [super dealloc];

}

#pragma mark - ------------ 单例 ---------------

+(DataBaseManager *)shareInstace{
  
    //全局只有一个
    static DataBaseManager *DBManager = nil;
    if (!DBManager) {
        DBManager = [[DataBaseManager alloc]init];
        
    }
    return  DBManager;

}
#pragma mark - ------------ 打开数据库 ---------------

-(void)openDataBase{
 
    if (_fmdb != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
   //获取数据库路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES).lastObject stringByAppendingPathComponent:@"fmdb.db"];
    NSLog(@"收藏数据库路径%@",path);
    //创建数据库对象
    self.fmdb = [[FMDatabase alloc]initWithPath:path];
    BOOL result = [_fmdb open];
    //判断
    if (result) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
}

#pragma mark - ------------ 创建表 删除表 ---------------
-(void)createTable{

   NSString *creatSQL = @"create table if not exists enjoyApp (number integer PRIMARY KEY Autoincrement, title text, productID text)";
    [self executeSQL:creatSQL message:@"创建表"];

}
#pragma mark - ------------删除表 ---------------
-(void)deleteTable
{
  NSString *deleteSQL = @"drop table enjoyApp";
    [self executeSQL:deleteSQL message:@"删除表"];

}
#pragma mark - ------------ 增 ---------------
- (void)insertEnjoy:(ModelOfFirst *)enjoy
{
    
    
    NSString *insertSQL = [NSString stringWithFormat:@"insert into enjoyApp(title, productID) values ('%@', '%@')", enjoy.short_name, enjoy.product_id];
    [self executeSQL:insertSQL message:@"插入数据"];
    
}

#pragma mark - ------------ 删 ---------------
- (void)deleteTitle:(NSString *)title
{
    
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from enjoyApp where title = '%@'", title];
    [self executeSQL:deleteSQL message:@"删除数据"];
}

#pragma mark - ------------ 改 ---------------
- (void)updateEnjoy:(ModelOfFirst *)newValue num:(NSInteger)num
{
    
    NSString *updateSQL = [NSString stringWithFormat:@"update enjoyApp set title = '%@', productID = '%@' where number = %ld", newValue.short_name, newValue.product_id, num];
    
    [self executeSQL:updateSQL message:@"修改数据"];
}

#pragma mark - ------------ 查 ---------------
- (NSMutableArray *)select
{
    
    NSMutableArray *mArr = [NSMutableArray array];
    //sql
    NSString *selectSQL = @"select * from enjoyApp";
    //执行查询 遍历每一行信息
    FMResultSet *resultSet = [_fmdb executeQuery:selectSQL];
    if(resultSet){
        
        
        while ([resultSet next]) {
            
            NSString *title = [resultSet stringForColumn:@"title"];
            NSString *productID = [resultSet stringForColumn:@"productID"];
            //创建对象
           ModelOfFirst *m = [[ModelOfFirst alloc]init];
            m.short_name = title;
            m.product_id = (NSNumber *)productID;
            
            [mArr addObject:m];
            
        }
        return mArr;
    }else{
        
        NSLog(@"查询未果");
        return nil;
    }
}


-(void)executeSQL:(NSString *)sql message:(NSString *)mes
{
    
    //执行
    BOOL result = [_fmdb executeUpdate:sql];
    if (result) {
        NSLog(@"%@成功", mes);
    }else{
        NSLog(@"%@失败", mes);
    }
    
    
}



@end
